//
//  ShaiXuanSelectView.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/2.
//  Copyright © 2016年 game. All rights reserved.
//

#import "ShaiXuanSelectView.h"

#import "ShaixuanCell.h"
#import "IslanDistance_typeModel.h"

#define chareactHeight (SCREEN_WIDTH/3-8*4)/3; //每个标签的高度

@interface ShaiXuanSelectView ()<UIGestureRecognizerDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,assign) CGFloat cellhight;
@property (nonatomic,assign) CGFloat backViewHeight;

@end

@implementation ShaiXuanSelectView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = TouMingBlack;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selfTap)];
        tap.delegate = self;
        [self addGestureRecognizer:tap];
        
        
    }
    return self;
}

-(void)setFiltType:(NSArray *)filtType{
    _filtType = filtType;
    
    
    self.backViewHeight = 0;
    /**
     *  获取 每行cell的高度 并相加  以取得白色视图BacView的高度
     */
    if (self.isyujudian) {
        
        NSArray *ary = [NSArray arrayWithObject:_filtType.lastObject];
        _filtType = [ary copy];
        
    }
    
    for (IslanDistance_typeModel *model in  _filtType) {
        
        ShaixuanCell *cell = [[ShaixuanCell alloc]init];
        
        cell.model = model;
        
        self.backViewHeight +=cell.cellhight;
    }

    
    self.backViewHeight += 60;
    if (self.tableview!=nil) {
        [self.tableview reloadData];
    }else{
        [self CreatBacView];
    }
}

-(void)selfTap{
    
    [self.deleget ShanXuanAlpha];
    self.alpha = 0;
}

/**
 *  点击backView 将键盘收起
 */
-(void)backViewTap{
    
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
}

-(void)CreatBacView{
    
    [self.tableview removeFromSuperview];
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.backViewHeight) style:UITableViewStylePlain];
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.scrollEnabled = NO;
    [self.tableview registerClass:[ShaixuanCell class] forCellReuseIdentifier:@"fujinCell"];
    [self addSubview:self.tableview];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.filtType.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShaixuanCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"fujinCell"];
    
    
    cell.model = self.filtType[indexPath.row];
   
    tableView.separatorStyle  = 0;
    cell.selectionStyle  = UITableViewCellSelectionStyleNone;
    
    self.cellhight = cell.cellhight;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 40;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return self.cellhight+1;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH-20, 30)];
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(10, 5, SCREEN_WIDTH-20, 40);
    
    btn.titleLabel.font = font(SCREEN_WIDTH*0.06);
    
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
     btn.backgroundColor = linColor;
    
    btn.layer.cornerRadius = 5;
    
    [btn addTarget:self action:@selector(sureClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [view addSubview:btn];
    
    return view;
}

//点击确定
-(void)sureClick:(UIButton *)btn{
    
     
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    for (int i=0; i<self.filtType.count; i++) {
        
        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:i inSection:0];
        
         ShaixuanCell *cell = [self.tableview cellForRowAtIndexPath:indexPath];
        
        if ([cell.selectTitle isEqualToString:@"show"]) {//选择了地图还是列表
            
            [self.deleget MapAndListChangeWhith:cell.selectString.integerValue];
            
            if (cell.selectString.integerValue==1) {//选择了地图
                
                 cell.model = self.filtType[indexPath.row];
            }
        }else{
        
            [dic addEntriesFromDictionary:@{cell.selectTitle:cell.selectString}];
        }
        
    }
    
    
    btn.backgroundColor = mainColor;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.deleget SureClickWhithDic:dic];
        self.alpha = 0;
    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                btn.backgroundColor = linColor;
    });
    
    
}

/**
 *  对某些手势进行 禁用
 */
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    // 若为UITableViewCellContentView（即点击了tableViewCell），则不截获Touch事件
    
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        
        return NO;
    }
    
    return YES;
}

@end
